<?php
namespace Admin\Controller;
use Think\Controller;
class CardController extends BaseController
{
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Card');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加优惠卡',
			'_page_btn_name' => '优惠卡列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Card');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Card');
    	$data = $model->find($id);
    	$this->assign('data', $data);

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改优惠卡',
			'_page_btn_name' => '优惠卡列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function delete()
    {
    	$model = D('Card');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Card');
    	$data = $model->search();
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '优惠卡列表',
			'_page_btn_name' => '添加优惠卡',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }

    /**
     *获取当前卡项的相关规则 课程设置
     */
    public function getnoclass()
    {
        $noc['id']  =I('id');
        $noclass = M('card')->where($noc)->getField("noclass");
        $arr = json_decode($noclass);
        if(!empty($arr))
        {
            $res['noclass'] = M("course")->field("course_name")->where(array("id"=>array("in",$arr)))->select();
        }else
        {
            $res['noclass'] = array();
        }

        $where['company_id'] = session("admin.company_id");
        $where['is_show'] ='1';
        $res['all'] = M("course")->field("id,course_name")->where($where)->select();
        die(json_encode($res));
    }

    /**
     *设置相关卡项规则 课程设置
     */
    public function setnoclass()
    {
        $where['id'] = I('cardtypeid');
        $where['company_id'] =session("admin.company_id");
        $res = M('card')->where($where)->find();
        if($res)
        {
            $data['noclass'] = json_encode(I('cardtypecourseCheck'));
            $result = M("card")->where($where)->save($data);
            if($result)
            {
                $this->success("设置成功！",U('lst'));
            }else
            {
                $this->error("设置失败，请重新设置！");
            }
        }else
        {
            $this->error("参数错误！请重试！");
        }
    }

    /**
     * 设置卡项规则 基础规则
     */
    public function setcardguize()
    {
        $where['id'] = I('cardTypeId');

        $where['company_id'] =session("admin.company_id");
        $res = M('card')->where($where)->find();
        if($res)
        {
            $arr['company_id'] = session("admin.company_id");
            $arr['canCross'] = "";
            $arr['dateOddEven'] = "";
            $arr['timeZone'] = "";
            $arr['weekDay'] = "";
            $arr['weekTimes'] = "";
            $arr['dayTimes'] = "";
            foreach (I('ruleCheckBox') as $v)
            {
                if($v=='timeZone')
                {
                    $r[0] = I('timeZoneBeginTime');
                    $r[1] = I('timeZoneEndTime');
                    $arr[$v] = json_encode($r);
                }elseif($v=='weekDay')
                {
                    $arr[$v] = json_encode(I("$v"));
                }else
                {
                    $arr[$v] = I($v);
                }
            }

            $result = M('CardGuize')->where(array("card_id"=>I('cardTypeId'),'company_id'=>$arr['company_id']))->save($arr);
            if($result)
            {
                $this->success("设置成功！",U('lst'));
            }else
            {
                $arr['card_id'] = I('cardTypeId');
                $aa = M('CardGuize')->data($arr)->add();
                if($aa)
                {
                    $this->success("设置成功！",U('lst'));
                }else
                {
                    $this->error("设置失败，请重新设置！");
                }

            }
        }else
        {
            $this->error("参数错误！请重试！");
        }
    }


    /**
     * ajax 获取以往规则
     */
    public function setguize()
    {
        $where['card_id'] = I('id');
        $where['company_id'] =session("admin.company_id");
        $res = M('CardGuize')->where($where)->find();

        $data = '';
        if($res)
        {
            if($res['cancross'])
            {
                $data .='<div class="row" id="canCross">';
                $data .='<div class="col-lg-3">跨门店限制</div><div class="col-lg-8">该卡是否可以跨门店使用';
                if($res['cancross']=='YES') $data .= '<span style="color: #457faa">是</span>';
                if($res['cancross']=='NO') $data .= '<span style="color: #aa6268">否</span>';
                $data .='</div>';
                $data .='</div>';
            }
            if($res['dateoddeven'])
            {
                $data .='<div class="row" id="dateOddEven">';
                $data .='<div class="col-lg-3">单双日限制</div><div class="col-lg-8">该卡仅限';
                if($res['dateoddeven']=='odd') $data .= '<span style="color: #457faa">单</span>';
                if($res['dateoddeven']=='even') $data .= '<span style="color: #aa6268">双</span>';
                $data .='日</div>';
                $data .='</div>';
            }
            if($res['timezone'])
            {
                $data .='<div class="row" id="timeZone">';
                $data .='<div class="col-lg-3">时间范围限制</div><div class="col-lg-8">该卡仅限';
                $resarr =json_decode($res['timezone']);
                $data .='该卡仅限'.$resarr['0'].'至'.$resarr['1'];
                $data .='</div>';
                $data .='</div>';
            }
            if($res['weekday'])
            {
                $data .='<div class="row" id="weekDay">';
                $data .='<div class="col-lg-3">一周日期限制</div><div class="col-lg-8">该卡仅限周';
                $resarrs =json_decode($res['weekday']);
                foreach ($resarrs as $v)
                {
                    $data .= $v.",&nbsp;&nbsp;";
                }
                $data .='</div>';
                $data .='</div>';
            }
            if($res['weektimes'])
            {
                $data .='<div class="row" id="weekTimes">';
                $data .='<div class="col-lg-3">一周多次限制</div><div class="col-lg-8">每周最多上课/预约';
                $data .=$res['weektimes'];
                $data .='次</div>';
                $data .='</div>';
            }
            if($res['daytimes']) {
                $data .='<div class="row" id="dayTimes">';
                $data .='<div class="col-lg-3">一天多次限制</div><div class="col-lg-8">每天最多上课/预约';
                $data .=$res['daytimes'];
                $data .='次</div>';
                $data .='</div>';
            }
        }else
        {
            $data .= '<div style="text-align: center;">暂无规则</div>';
        }

        die(json_encode($data));
    }
}